package cn.mymall.service.impl;

import cn.mymall.entity.UserMenu;
import cn.mymall.mapper.UserMenuMapper;
import cn.mymall.service.IUserMenuService;
import cn.mymall.support.ResultCode;
import cn.mymall.vo.menu.UserMenuReqVo;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sky
 * @since 2021-03-11
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class UserMenuServiceImpl extends ServiceImpl<UserMenuMapper, UserMenu> implements IUserMenuService {
        private Logger dbLogger = LoggerFactory.getLogger(this.getClass());
        @Override
        @Transactional
        public ResultCode setUserMenu(UserMenuReqVo reqVo) {
                UpdateWrapper updateWrapper = new UpdateWrapper();
                updateWrapper.eq("user_id", reqVo.getUserId());
                remove(updateWrapper);

                List<Integer> menuIdList = reqVo.getMenuIdList();
                dbLogger.warn(StringUtils.join(menuIdList, ","));
                if (CollectionUtils.isNotEmpty(menuIdList)) {
                        for (Integer menuId : menuIdList) {
                                UserMenu userMenu = new UserMenu();
                                userMenu.setMenuId(menuId);
                                userMenu.setUserId(reqVo.getUserId());
                                save(userMenu);
                        }
                }
                return ResultCode.SUCCESS;
        }
}
